package Control;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import Entity.*;
import GUI.ExtensionRequest_GUI;
import GUI.ScreenManager;

/**
 * Controls the instance of Extention Stage 
 * This class sends and gets data from the server 
 * and display it on the panel.
 * @author Aviat
 *
 */ 
public class ExtentionStage {

	private ExtensionRequest_GUI ext_stage_gui;
	private MsgPackage mess;
	private Extention_Stage_req  extention_stage_req;
	private Object result;
	
	/**
	 * the constructor
	 */
	public ExtentionStage() {
		ext_stage_gui = new ExtensionRequest_GUI();
		mess= new MsgPackage();
		extention_stage_req= new Extention_Stage_req();
		result = new Object();
	}

	/**
	 * 
	 * @return ExtentionStageGui
	 */
	public ExtensionRequest_GUI getExt_stage_gui(){
		return ext_stage_gui;
	}
	
	/**
	 * set the request and stage number in the extention entity
	 */
	public void reqANDstageEntered(String Req, String Stage){
		extention_stage_req.setR_id(Integer.parseInt(Req));
		extention_stage_req.setStageName(Stage);
		
	}
	
	/**
	 * This method sends the extention details 
	 *  the server sends an answer (received from query from database) 
	 *  and the control display it in a message
	 */
	public boolean extSubb_pressed(int id, String name){
        Integer x =Integer.parseInt(ext_stage_gui.getNumDays().getSelectedItem().toString());
		String y =ext_stage_gui.getArgument().getText();
		if(x==null||y==null){
			JOptionPane.showMessageDialog(null, "Not all fileds are fill  ", "ERROR", JOptionPane.ERROR_MESSAGE);
			return false;
		}
		extention_stage_req.setR_id(id);
		extention_stage_req.setStageName(name);
		extention_stage_req.setNumDay(x);
		extention_stage_req.setArrgumant(y);
		mess.setObject(extention_stage_req);
		mess.setOp_code(OP_CODE.EXTENTION_STAGE);
		ScreenManager.getConnection().accept(mess);
		
		//while(ScreenManager.getConnection().getAnswerReceivedFlag() == false);	//wait until flag is changed and shows that result was received from server
		Object o = ScreenManager.getConnection().cclient.getMessage();

    	//Object o = ScreenManager.getConnection().getResultobject();
    	result = o;
    	if((Integer)result==0)
    		JOptionPane.showMessageDialog(null, "The stage was extended before", "ERROR", JOptionPane.ERROR_MESSAGE);
    	else if((Integer)result==1)
    		JOptionPane.showMessageDialog(null, "There are lest the 3 days till clouser stage date  ", "ERROR", JOptionPane.ERROR_MESSAGE);
    	else if((Integer)result==2)
    		JOptionPane.showMessageDialog(null, "The stage is not open", "ERROR", JOptionPane.ERROR_MESSAGE);
    	else
    		JOptionPane.showMessageDialog(null, "Your request has been sent for approval ", "", JOptionPane.INFORMATION_MESSAGE);    
		return true;
	}
}
